package org.graduates.webapp.actions.course.json;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.graduates.webapp.actions.BaseAction;
import org.projects.graduates.domain.Person;
import org.projects.graduates.domain.Studying;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 返回某用户所修的所有课程
 * 
 * @author Administrator
 * 
 */
@ParentPackage("web")
public class GetStudiedAction extends BaseAction {

	private static final long serialVersionUID = 8381374234275872693L;

	private List<Studying> studied = new ArrayList<Studying>();
	
	private long pid = 0;

	@Action(results = { @Result(name = "success", type = "json", params = {
			"includeProperties",
			"studied\\[\\d+\\]\\.course," + "studied\\[\\d+\\]\\.course.name,"  + "studied\\[\\d+\\]\\.course.content,"
			+ "studied\\[\\d+\\]\\.course.id,"
			+ "studied\\[\\d+\\]\\.id" }) })
	public String execute() {

		if(pid < 1){
			studied.addAll(Studying.findBy(getCurrentPerson()));
		}else{
			Person person = Person.get(Person.class, pid);
			if(null == person){
				studied.addAll(Studying.findBy(person));
			}
		}

		return ActionSupport.SUCCESS;
	}

	public List<Studying> getStudied() {
		return studied;
	}

	public void setStudied(List<Studying> studied) {
		this.studied = studied;
	}

}
